<template>
  <div class="min-match-container">
    <div class="matchs-item" :key="index" v-for="(item, index) in matchList">
      <div class="matchs-title" @click="showDetail(item)">
        {{item.title}}
        <img
          :class="{active: item.showDetail}"
          src="../../../assets/imgs/live/touzhu_icon_shangla.png"
          alt
        >
      </div>
      <div class="matchs-detail" v-if="item.showDetail">
        <div class="match-item" :key="i" v-for="(match, i) in item.matches_list">
          <div class="match-info">
            <div>{{match.issue_num_str}}</div>
            <div class="middle">{{match.league_name}}</div>
            <div>{{match.match_time_str}}</div>
          </div>
          <div class="match-content">
            <div class="match-score">
              {{match.home_team_name}}（主）
              <span>VS</span>
              {{match.guest_team_name}}（客）
            </div>
            <div class="match-select">
              <div class="items">
                <div class="item-row" v-if="match.spf_sell_status===1">
                  <div class="spf-num">0</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['spf'] || {})[0] === (match.spf[0]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'spf', issue_num: match.issue_num, i: 0, value:(match.spf[0]||[])[2]})"
                  >{{(match.spf[0]||[])[1]+' '+(match.spf[0]||[])[2]}}</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['spf'] || {})[1] === (match.spf[1]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'spf', issue_num: match.issue_num, i: 1, value:(match.spf[1]||[])[2]})"
                  >{{(match.spf[1]||[])[1]+' '+(match.spf[1]||[])[2]}}</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['spf'] || {})[2] === (match.spf[2]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'spf', issue_num: match.issue_num, i: 2, value:(match.spf[2]||[])[2]})"
                  >{{(match.spf[2]||[])[1]+' '+(match.spf[2]||[])[2]}}</div>
                </div>
                <div class="empty-row" v-else>未开售</div>
                <div class="item-row row2" v-if="match.rq_sell_status===1">
                  <div class="rq-num">{{match.concede_num}}</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['rq'] || {})[0] === (match.rq[0]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'rq', issue_num: match.issue_num, i: 0, value:(match.rq[0]||[])[2]})"
                  >{{(match.rq[0]||[])[1]+' '+(match.rq[0]||[])[2]}}</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['rq'] || {})[1] === (match.rq[1]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'rq', issue_num: match.issue_num, i: 1, value:(match.rq[1]||[])[2]})"
                  >{{(match.rq[1]||[])[1]+' '+(match.rq[1]||[])[2]}}</div>
                  <div
                    :class="{active: (((selectedMatchs[match.nm_matches_id] || {})[match.issue_num] || {})['rq'] || {})[2] === (match.rq[2]||[])[2] ? 'active' : '', item: true}"
                    @click="selectMatch({nm_matches_id: match.nm_matches_id, type: 'rq', issue_num: match.issue_num, i: 2, value:(match.rq[2]||[])[2]})"
                  >{{(match.rq[2]||[])[1]+' '+(match.rq[2]||[])[2]}}</div>
                </div>
                <div class="empty-row" v-else>未开售</div>
              </div>
              <div @click="selectBatchMatch(match)" class="expend" v-if="selectNum(match)===0">展开全部</div>
              <div @click="selectBatchMatch(match)" class="expend active" v-else>
                已选
                <br>
                {{selectNum(match)}}个
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <confirm
      v-if="showSelectModal"
      v-model="showSelectModal"
      @on-cancel="showSelectModal=false"
      @on-confirm="confirmSelectModal"
      class="zq-min-match-modal"
    >
      <div class="title">
        {{selectedMatch.home_team_name}}
        <img
          class="logo"
          src="../../../assets/imgs/live/zhu.png"
          alt
        >
        <span>VS</span>
        {{selectedMatch.guest_team_name}}
        <img
          class="logo"
          src="../../../assets/imgs/live/ke.png"
          alt
        >
      </div>
      <div class="alert">主队【{{selectedMatch.home_team_name}}】让球{{selectedMatch.concede_num}}</div>
      <div class="detail">
        <div class="spf" v-if="selectedMatch.spf_sell_status===1">
          <div class="left">0</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in selectedMatch.spf"
              :class="{active: (copybf.spf || {})[mindex]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('spf', mindex, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="rq" v-if="selectedMatch.rq_sell_status===1">
          <div class="left">{{selectedMatch.concede_num}}</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in selectedMatch.rq"
              :class="{active: (copybf.rq || {})[mindex]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('rq', mindex, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="s" v-if="selectedMatch.bf_sell_status===1">
          <div class="left">主胜</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in (selectedMatch.bf || []).slice(0, 13)"
              :class="{active: (copybf.bf || {})[mindex]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('bf', mindex, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="p" v-if="selectedMatch.bf_sell_status===1">
          <div class="left">平</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in (selectedMatch.bf || []).slice(13, 18)"
              :class="{active: (copybf.bf || {})[mindex+13]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('bf', mindex+13, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="f" v-if="selectedMatch.bf_sell_status===1">
          <div class="left">主负</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in (selectedMatch.bf || []).slice(18, 31)"
              :class="{active: (copybf.bf || {})[mindex+18]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('bf', mindex+18, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="jq" v-if="selectedMatch.jq_sell_status===1">
          <div class="left">总进球</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in selectedMatch.jq"
              :class="{active: (copybf.jq || {})[mindex]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('jq', mindex, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
        <div class="bqc" v-if="selectedMatch.bqc_sell_status===1">
          <div class="left">半全场</div>
          <div class="right">
            <div
              :key="mindex"
              v-for="(mitem, mindex) in selectedMatch.bqc"
              :class="{active: (copybf.bqc || {})[mindex]===mitem[2] ? 'active' : '', item: true}"
              @click="onMatchSelect('bqc', mindex, mitem[2])"
            >
              <div class="jq-num">{{mitem[1]}}</div>
              <div class="score">{{mitem[2]}}</div>
            </div>
          </div>
        </div>
      </div>
    </confirm>
  </div>
</template>
<script>
import { Confirm } from 'vux';
import values from 'lodash/values';
import keys from 'lodash/keys';

export default {
  components: {
    Confirm
  },
  props: {
    matchList: {
      type: Array
    },
    selectedMatchs: {
      type: Object
    }
  },
  data () {
    return {
      showSelectModal: false,
      copybf: {},
      selectedMatch: {}
    };
  },
  methods: {
    showDetail (item) {
      item.showDetail = !item.showDetail;
      this.$forceUpdate();
    },
    selectMatch (data) {
      this.$emit('selectMatch', data);
    },
    selectBatchMatch (match) {
      const { nm_matches_id, issue_num } = match;
      const selectedall =
        this.selectedMatchs[nm_matches_id] &&
        this.selectedMatchs[nm_matches_id][issue_num]
          ? this.selectedMatchs[nm_matches_id][issue_num]
          : {};
      this.copybf = { ...selectedall };
      this.selectedMatch = match;
      this.showSelectModal = true;
    },
    confirmSelectModal () {
      const data = { ...this.copybf };
      const { nm_matches_id, issue_num } = this.selectedMatch;
      this.$emit('selectAllBatchMatch', {
        nm_matches_id,
        issue_num,
        data
      });
    },
    onMatchSelect (type, index, value) {
      if (!this.copybf[type]) {
        const typeObj = {
          [index]: value
        };
        this.$set(this.copybf, type, typeObj);
      } else {
        if (this.copybf[type][index]) {
          this.$delete(this.copybf[type], index);
        } else {
          this.$set(this.copybf[type], index, value);
        }
      }
    },
    selectNum ({ nm_matches_id, issue_num }) {
      if (!this.selectedMatchs[nm_matches_id]) {
        return 0;
      }
      if (!this.selectedMatchs[nm_matches_id][issue_num]) {
        return 0;
      }
      return values(this.selectedMatchs[nm_matches_id][issue_num]).reduce(
        (t, c) => {
          t += keys(c).length;
          return t;
        },
        0
      );
    }
  }
};
</script>
<style lang="less">
.min-match-container {
  .matchs-item {
    font-size: 0.24rem;
    .matchs-title {
      height: 0.6rem;
      line-height: 0.6rem;
      background: white;
      text-align: center;
      position: relative;
      img {
        position: absolute;
        right: 0.2rem;
        top: 0.16rem;
        transform: rotate(-180deg);
        height: 0.16rem;
        width: 0.3rem;
        &.active {
          transform: rotate(0deg);
        }
      }
    }
    .matchs-detail {
      background: #fff3f4;
      .match-item {
        display: flex;
        border-bottom: 1px solid #d2d2d2;
        .match-info {
          width: 1.2rem;
          padding: 0.2rem;
          color: #202020;
          font-size: 0.24rem;
          .middle {
            margin-top: 0.1rem;
            margin-bottom: 0.1rem;
          }
        }
        .match-content {
          // width: 5.8rem;
          width: 100%;
          padding: 0.2rem;
          .match-score {
            text-align: center;
            margin-bottom: 0.1rem;
            span {
              color: #8a8a8a;
              padding-right: 0.1rem;
            }
          }
          .match-select {
            display: flex;
            .items {
              flex: 1;
              .empty-row {
                height: 0.54rem;
                line-height: 0.54rem;
                // width: 3.94rem;
                flex: 1;
                text-align: center;
                background: #fff;
                border: 1px solid #d2d2d2;
                &:nth-child(2) {
                  border-top: none;
                }
              }
              .item-row {
                display: flex;
                &.row2 {
                  .item {
                    border-top: none;
                  }
                }
                .spf-num {
                  background: #4fb1f8;
                  color: #fff;
                  width: 0.4rem;
                  text-align: center;
                  line-height: 0.56rem;
                }
                .rq-num {
                  background: #ed752f;
                  width: 0.4rem;
                  color: #fff;
                  text-align: center;
                  line-height: 0.56rem;
                }
                .item {
                  height: 0.54rem;
                  // width: 1.2rem;
                  flex: 1;
                  text-align: center;
                  background: #fff;
                  border: 1px solid #d2d2d2;
                  border-right: none;
                  line-height: 0.54rem;
                  font-size: 0.2rem;
                  &.active {
                    background: #d44743;
                    color: #fff;
                  }
                  &:last-child {
                    border-right: 1px solid #d2d2d2;
                  }
                }
              }
            }
            .expend {
              display: inline-block;
              padding: 0.3rem 0.2rem;
              background: #fff;
              border: 1px solid #d2d2d2;
              border-left: none;
              width: 0.6rem;
              &.active {
                background: #d44743;
                color: #fff;
              }
            }
          }
        }
      }
    }
  }
  .zq-min-match-modal {
    .title {
      font-size: 0.3rem;
      img {
        width: 0.28rem;
      }
      span {
        margin: 0 0.4rem;
      }
    }
    .alert {
      text-align: left;
      color: #b9b9b9;
      font-size: 0.18rem;
      margin-top: 0.2rem;
      margin-bottom: 0.1rem;
    }
    .detail {
      display: flex;
      font-size: 0.2rem;
      width: 5.2rem;
      flex-wrap: wrap;
      height: 4rem;
      overflow-y: auto;
      .spf {
        // width: 5.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #4fb1f8;
          height: 0.6rem;
          color: #fff;
          line-height: 0.6rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 32.6%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
            // &:last-child {
            //   width: 1.36rem;
            // }
          }
        }
      }
      .rq {
        // width: 5.2rem;
        margin-bottom: 0.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #ed752f;
          height: 0.54rem;
          color: #fff;
          padding-top: 0.06rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 32.6%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
            // &:last-child {
            //   width: 1.36rem;
            // }
          }
        }
      }
      .s {
        // width: 5.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #c99feb;
          height: 0.9rem;
          color: #fff;
          padding-top: 0.3rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 14%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
            &:last-child {
              width: 28%;
            }
          }
        }
      }
      .p {
        margin-top: 0.2rem;
        margin-bottom: 0.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #d0dffc;
          height: 0.4rem;
          color: #fff;
          padding-top: 0.2rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 14%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            // border: 1px solid #d2d2d2;
            &.active {
              background: #d44743;
              color: #fff;
            }
            // .jq {
            //   font-size: 0.2rem;
            // }
            .score {
              font-size: 0.16rem;
            }
            &:last-child {
              width: 42%;
            }
          }
        }
      }
      .f {
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #7adf89;
          height: 0.9rem;
          color: #fff;
          padding-top: 0.3rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 14%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #d5f6da;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
            &:last-child {
              width: 28%;
            }
          }
        }
      }
      .jq {
        // width: 5.2rem;
        margin-top: 0.2rem;
        margin-bottom: 0.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #c99feb;
          height: 1rem;
          color: #fff;
          padding-top: 0.2rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 24.5%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
          }
        }
      }
      .bqc {
        // width: 5.2rem;
        display: flex;
        width: 100%;
        .left {
          width: 0.2rem;
          padding-left: 0.05rem;
          padding-right: 0.05rem;
          display: inline-block;
          background: #d0dffc;
          height: 1rem;
          color: #fff;
          padding-top: 0.2rem;
        }
        .right {
          display: inline-block;
          width: 100%;
          text-align: left;
          .item {
            display: inline-block;
            text-align: center;
            width: 19.6%;
            height: 0.5rem;
            padding-top: 0.1rem;
            background: #fbf3f4;
            &.active {
              background: #d44743;
              color: #fff;
            }
            .score {
              font-size: 0.16rem;
            }
          }
        }
      }
    }
  }
}
</style>
